local skel = fk.CreateSkill {
  name = "lb__huaqi",
  dynamic_desc = function(self, player, lang)
    if player:getMark("lb__shengjie") > 0 then
      return
      --"你与成为过<a href=':lb__huaqi'><font color='red'>〖花祈〗</font></a>目标的角色于每轮首次造成伤害后可以摸X张牌（X为成为过<a href=':lb__huaqi'><font color='red'>〖花祈〗</font></a>目标的存活角色数）。"
      "每轮限X次，你或成为过<a href=':lb__huaqi'><font color='red'>〖花祈〗</font></a>目标的角色受到伤害后，你可以令其摸X张牌（X为成为过<a href=':lb__huaqi'><font color='red'>〖花祈〗</font></a>目标的角色数）。"
    end
    return "每项限一次，你可以令一名角色于以下时机回复1点体力：<br>" ..
        "1.体力值减少至2后；<br>" ..
        "2.进入濒死时；<br>" ..
        "3.没有手牌后。"
  end,
  max_branches_use_time = function(self, player)
    local list = {
      ["lose"] = {
        [Player.HistoryGame] = 1
      },
      ["dying"] = {
        [Player.HistoryGame] = 1
      },
      ["kongcheng"] = {
        [Player.HistoryGame] = 1
      },
      ["drawcard"] = {
        [Player.HistoryRound] = player:getMark("lb__shengjie") == 0 and 0 or #player:getTableMark("@[player]lb__huaqi")
      },
    }
    return list
  end
}

Fk:loadTranslationTable {
  ["lb__huaqi"] = "花祈",
  [":lb__huaqi"] = "每项限一次，你可以令一名角色于以下时机回复1点体力：<br>" ..
      "1.体力值减少至2后；<br>" ..
      "2.进入濒死时；<br>" ..
      "3.没有手牌后。",

  ["@[skill_choices]lb__huaqi"] = "花祈",
  ["@@lb__huaqi"] = "花祈",
  ["@[player]lb__huaqi"] = "花祈",
  ["#lb__huaqi"] = "花祈：你可以令%dest回复1点体力",
  ["#lb__huaqi-draw"] = "花祈：你可以令 %dest 摸%arg张牌",

  ["$lb__huaqi1"] = "白花盛放。",
  ["$lb__huaqi2"] = "领受天赐。",
}

--skel:addEffect(fk.Damage, {
skel:addEffect(fk.Damaged, {
  anim_type = "drawcard",
  times = function(self, player)
    if player:getMark("lb__shengjie") == 0 then
      local num = 0
      for _, choice in ipairs({"lose", "dying", "kongcheng"}) do
        if skel:withinBranchTimesLimit(player, choice, Player.HistoryGame) then
          num = num + 1
        end
      end
      return num
    end
    return #player:getTableMark("@[player]lb__huaqi") - player:usedEffectTimes(self.name, Player.HistoryRound)
  end,
  can_trigger = function(self, event, target, player, data)
    --local event_id = player.room.logic:getCurrentEvent().id
    return player:hasSkill(skel.name) and player:getMark("lb__shengjie") > 0 and target:isAlive()
        --data.from and target == data.from and
        and (target == player or table.contains(player:getTableMark("@[player]lb__huaqi"), target.id)) 
        and skel:withinBranchTimesLimit(player, "drawcard", Player.HistoryRound)
    --[[
        (#target.room.logic:getEventsOfScope(GameEvent.Damage, 1, function(e)
          local damage = e.data
          --return use.from == target and e.id ~= event_id
          return damage.to == target and e.id ~= event_id
        end, Player.HistoryRound) == 0)
        --]]
  end,
  on_cost = function(self, event, target, player, data)
    --[[
    local n = #table.filter(player.room:getAlivePlayers(), function(p)
      return p:getMark("@@lb__huaqi") > 0
    end)--]]
    local n = #player:getTableMark("@[player]lb__huaqi")
    event:setCostData(self, { n = n })
    return n > 0 and
        target.room:askToSkillInvoke(player,
          { skill_name = skel.name, prompt = "#lb__huaqi-draw::" .. target.id .. ":" .. n })
  end,
  on_use = function(self, event, target, player, data)
    player:addSkillBranchUseHistory(skel.name, "drawcard")
    target:drawCards(event:getCostData(self).n, skel.name)
  end,
})

skel:addEffect(fk.HpChanged, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and player:getMark("lb__shengjie") == 0
        and skel:withinBranchTimesLimit(player, "lose", Player.HistoryGame)
        and data.num < 0 and target:isWounded() and target.hp == 2
  end,
  on_cost = function(self, event, target, player, data)
    event:setCostData(self, { tos = { target } })
    return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#lb__huaqi::" .. target.id })
  end,
  on_use = function(self, event, target, player, data)
    player:addSkillBranchUseHistory(skel.name, "lose")
    local room = player.room
    room:addTableMarkIfNeed(player, "@[player]lb__huaqi", target.id)
    room:recover({
      who = target,
      num = 1,
      recoverBy = player,
      skillName = "lb__huaqi",
    })
  end,
})

skel:addEffect(fk.EnterDying, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and player:getMark("lb__shengjie") == 0
        and skel:withinBranchTimesLimit(player, "dying", Player.HistoryGame)
        and target:isWounded()
  end,
  on_cost = function(self, event, target, player, data)
    event:setCostData(self, { tos = { target } })
    return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#lb__huaqi::" .. target.id })
  end,
  on_use = function(self, event, target, player, data)
    player:addSkillBranchUseHistory(skel.name, "dying")
    local room = player.room
    room:addTableMarkIfNeed(player, "@[player]lb__huaqi", target.id)
    room:recover({
      who = target,
      num = 1,
      recoverBy = player,
      skillName = "lb__huaqi",
    })
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and skel:withinBranchTimesLimit(player, "kongcheng", Player.HistoryGame)
        and player:getMark("lb__shengjie") == 0 then
      local kc = false
      for _, move in ipairs(data) do
        if move.from then
          local t = move.from
          if t.dead or not t:isKongcheng() or not t:isWounded() then return false end
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              event:setCostData(self, { tos = { t } })
              kc = true
              break
            end
          end
        end
      end
      return kc
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player,
      { skill_name = skel.name, prompt = "#lb__huaqi::" .. event:getCostData(self).tos[1].id })
  end,
  on_use = function(self, event, target, player, data)
    player:addSkillBranchUseHistory(skel.name, "kongcheng")
    local to = event:getCostData(self).tos[1]
    local room = player.room
    room:addTableMarkIfNeed(player, "@[player]lb__huaqi", to.id)
    room:recover({
      who = to,
      num = 1,
      recoverBy = player,
      skillName = "lb__huaqi",
    })
  end,
})

skel:addAcquireEffect(function(self, player, is_start)
  local list = {
    ["name"] = skel.name,
    ["scope"] = Player.HistoryGame,
    ["choices"] = { { "lose" }, { "dying" }, { "kongcheng" } }
  }
  player.room:setPlayerMark(player, "@[skill_choices]lb__huaqi", list)
end)

skel:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@[skill_choices]lb__huaqi", 0)
  player.room:setPlayerMark(player, "lb__shengjie", 0)
end)

return skel
